home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / LTOA.C < prev    next >
C/C++ Source or Header  |  1986-05-18  |  2KB  |  78 lines

  1. /* 1.0  05-07-85                        (ltoa.c)
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1985        *
  6.  ************************************************************************
  7.  *
  8.  *    These functions are modified versions of itoa() found in Kernighan
  9.  *    and Ritchie, page 60, with modifications of exercises 3-3 and 3-5,
  10.  *    for use with the long data type.
  11.  *
  12.  *----------------------------------------------------------------------*/
  13.  
  14. #include "defs.h"
  15. #include "stdtyp.h"
  16. #include "errno.h"
  17.  
  18. /************************************************************************/
  19.     STRING
  20. ltoa(s, n, w)
  21.  
  22. /*----------------------------------------------------------------------*/
  23. STRING s;
  24. long n;
  25. {
  26.     STRING ltoab();
  27.  
  28.     return ltoab(s, n, w, 10);
  29. }
  30. /*\p*********************************************************************/
  31.     STRING
  32. ltoab(s, n, w, b)    /* convert n to a minimum of |w| ascii characters,
  33.                base b, and put in s.  Use 0-fill if w < 0.
  34.                Return pointer to s.                */
  35. /*----------------------------------------------------------------------*/
  36. STRING s;
  37. long n;
  38. {
  39.     LOCAL char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  40.     int i, j;
  41.     BOOL sign;
  42.     char c, fill;
  43.  
  44.     if (n < 0L)
  45.     {    n = -n;
  46.         sign = TRUE;
  47.     }
  48.     else
  49.         sign = FALSE;
  50.     if (w < 0)
  51.     {    fill = '0';
  52.         w = -w;
  53.     }
  54.     else
  55.         fill = ' ';
  56.     i = 0;
  57.     do            /* generate digits in reverse order:    */
  58.         s[i++] = digits[(int)(n % b)];
  59.     while (n /= b);
  60.     if (sign)            /* put the sign into the string.*/
  61.         s[j = i++] = '-';
  62.     else
  63.         j = i - 1;
  64.     while (i < w)            /* expand to proper width.    */
  65.         s[i++] = fill;    /* i is just beyond last filled postion    */
  66.     if (--i ISNT j AND sign AND fill IS '0')
  67.     {    s[i] = s[j];        /* get the sign.        */
  68.         s[j] = fill;        /* fill in over it        */
  69.     }
  70.     s[++i] = NULL;            /* add null terminator    */
  71.     for (i--, j = 0; j < i; j++, i--) /*  now reverse chars */
  72.     {    c = s[j];
  73.         s[j] = s[i];
  74.         s[i] = c;
  75.     }
  76.     return s;
  77. }
  78.